package com.woniu.serverdriveruser.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.internalcommon.dto.ApiBossCar;
import com.woniu.internalcommon.remote.ServiceMapClient;
import com.woniu.internalcommon.response.TerminalResponse;
import com.woniu.internalcommon.response.TrackResponse;


import com.woniu.internalcommon.dto.Car;
import com.woniu.internalcommon.dto.ResponseResult;
import com.woniu.serverdriveruser.mapper.CarMapper;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class CarService {

    @Autowired
    private CarMapper carMapper;

    @Resource
    private ServiceMapClient serviceMapClient;


    /**
     *  添加车辆
     * @param car
     */
    public ResponseResult add(Car car) {
        car.setGmtCreate(LocalDateTime.now());
        car.setGmtModified(LocalDateTime.now());
        // 保存车辆
        carMapper.insert(car);
        // 获得此车辆的终端id：tid
        ResponseResult<TerminalResponse> terminalResult = serviceMapClient.addTerminal(car.getVehicleNo(), car.getId() + "");
        String tid = terminalResult.getData().getTid();
        car.setTid(tid);

        // 获得此车辆的轨迹id：trid
        ResponseResult<TrackResponse> track = serviceMapClient.addTrack(tid, car.getId() + "");
        String trid = track.getData().getTrid();
        String trname = track.getData().getTrname();
        car.setTrid(trid);
        car.setTrname(trname);

        carMapper.updateById(car);
        return ResponseResult.success("添加车辆成功");
    }

    /**
     *  根据id查询车辆
     * @param id
     * @return
     */
    public ResponseResult<Car> selectById(long id) {
        return ResponseResult.success(carMapper.selectById(id));
    }

    /**
     *  查询所有车辆
     * @param apiBossCar
     * @return
     */
    public ResponseResult<Page<Car>> getAllCar(ApiBossCar apiBossCar) {
        Page<Car> page = new Page<>();
        page.setCurrent(apiBossCar.getPageNum());
        page.setSize(apiBossCar.getPageSize());
        QueryWrapper<Car> carQueryWrapper = new QueryWrapper<>();
        Car car = apiBossCar.getCar();
        if (car.getAddress()!=null && !car.getAddress().isEmpty()){
            carQueryWrapper.like("address",car.getAddress());
        }else if (car.getVehicleNo()!=null && !car.getVehicleNo().isEmpty()) {
            carQueryWrapper.like("vehicle_no",car.getVehicleNo());
        } else if (car.getOwnerName()!=null&& !car.getOwnerName().isEmpty()) {
            carQueryWrapper.like("owner_name",car.getOwnerName());
        }else if (car.getState()!=null){
            carQueryWrapper.eq("state",car.getPlateColor());
        } else if (car.getId()!=null) {
            carQueryWrapper.eq("id",car.getId());
        }
        Page<Car> carPage = carMapper.selectPage(page, carQueryWrapper);
        return ResponseResult.success(carPage);
    }
}
